package ServletDemo2;//package ServletDemo01;


import entity.Books;
import util.GokJDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Servlet implementation class StudentEditServlet
 */
@WebServlet("/BooksEdit")
public class BooksEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BooksEdit() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 获取传递的图书 ID 参数
		String bookId = request.getParameter("bookId");

		// 根据图书 ID 查询图书详细信息
		String query = "SELECT * FROM books WHERE bookId = ?";
		Books books = GokJDBCUtil.queryForObject(query, Books.class, bookId);

		// 将查询到的图书信息设置为请求属性，并转发到编辑页面
		if (books != null) {
			request.setAttribute("books", books);
			request.getRequestDispatcher("books-edit.jsp").forward(request, response);
		} else {
			// 如果未找到该图书，跳转到错误页面或首页
			response.sendRedirect("home.jsp?error=未找到该图书");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 * 处理 POST 请求：更新图书信息
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		// 获取表单提交的数据
		String bookId = request.getParameter("bookId");
		String bookName = request.getParameter("bookName");
		String category = request.getParameter("category");
		String author = request.getParameter("author");
		String publisher = request.getParameter("publisher");
		String language = request.getParameter("language");
		double price = Double.parseDouble(request.getParameter("price"));
		String publishDate = request.getParameter("publishDate");

		// 校验字段是否完整
		if (bookId == null || bookName == null) {
			response.sendRedirect("books-edit.jsp?bookId=" + bookId + "&error=缺少必填字段");
			return;
		}

		// 执行数据库更新操作
		String updateQuery = "UPDATE books SET bookName = ?, category = ?, author = ?, publisher = ?, language = ?, price = ?, publish_date = ? WHERE bookId = ?";
		int result = GokJDBCUtil.update(updateQuery, bookName, category, author, publisher, language, price, publishDate, bookId);

		System.out.println("bookId: " + bookId);
		System.out.println("bookName: " + bookName);
		System.out.println("category: " + category);
		System.out.println("author: " + author);
		System.out.println("publisher: " + publisher);
		System.out.println("language: " + language);
		System.out.println("price: " + price);
		System.out.println("publishDate: " + publishDate);

		// 根据更新结果决定跳转的页面
		if (result > 0) {
			response.sendRedirect("home.jsp");
		} else {
			response.sendRedirect("books-edit.jsp?bookId=" + bookId + "&error=更新失败");
		}
	}
}
